package com.cnspt.controllers;

import com.cnspt.models.Users;
import com.cnspt.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("users")
public class UsersController {
    @Autowired
    private UsersRepository usersRepository;

    @RequestMapping("getUsers")
    public List<Users> getUsers() {
        List<Users> listUsers = (List<Users>) usersRepository.findAll();
        return listUsers;
    }

    @RequestMapping("addUser")
    public String addUser() {
        Users data = new Users();
        data.setName("吕志强");
        data.setMobile("15949411455");
        data.setSex(1);
        Users datas = usersRepository.save(data);
        System.out.println(datas.toString());
        return "ok";
    }
    @RequestMapping("delUser")
    public String delUser() {
        usersRepository.deleteById(9);
        return "ok";
    }
    @RequestMapping("updateUser")
    public String updateUser() {
        Users data = usersRepository.findById(10).get();
        data.setName("郭佳利");
        data.setSex(2);
        data.setMobile("15012345678");
        usersRepository.save(data);
        return "ok";
    }
}
